import Event

class MotionOperator:
    def __init__(self):
        pass
    
    def Stretch(self):
        raise NotImplementedError("Method MotionExer::Stretch not implemented.")
    
    def Shrink(self):
        raise NotImplementedError("Method MotionExer::Shrink not implemented.")
    
    def Stop(self):
        raise NotImplementedError("Method MotionExer::Stop not implemented.")
    
class DoubleControl(MotionOperator): # shuang lian kong
    def __init__(self, v_stretch, v_shrink):
        MotionOperator.__init__(self)
        self.v_stretch = v_stretch
        self.v_shrink = v_shrink
        
    def Stretch(self):
        self.v_shrink.SetValue(False)
        self.v_stretch.SetValue(True)
    
    def Shrink(self):
        self.v_stretch.SetValue(False)
        self.v_shrink.SetValue(True)
    
    def Stop(self):
        self.v_stretch.SetValue(False)
        self.v_shrink.SetValue(False)
        
class SingleControl(MotionOperator):# dan lian kong
    def __init__(self, v_value):
        MotionOperator.__init__(self)
        self.v_value = v_value
        
    def Stretch(self):
        self.v_value.SetValue(True)
        
    def Shrink(self):
        self.v_value.SetValue(False)
        
    def Stop(self):
        pass
    
class AirCylinder:
    def __init__(self, controler, v_limit, v_home):
        self.controler = controler
        self.v_limit = v_limit
        self.v_home = v_home
        
        self._Init()
        
    def _Init(self):
        if self.v_limit:
            self.v_limit.Bind(Event.EVT_POSITIVE_EDGE, self.OnLimitEnter)
        if self.v_home:
            self.v_home.Bind(Event.EVT_POSITIVE_EDGE, self.OnHomeEnter)
        
    def Stretch(self):
        self.controler.Stretch()
        
    def Shrink(self):
        self.controler.Shrink()
        
    def Stop(self):
        self.controler.Stop()
        
    def IsLimit(self):
        if self.v_limit:
            return self.v_limit.GetValue()
        elif self.v_home:
            return not self.v_home.GetValue()
        else:
            return True
        
    def IsHome(self):
        if self.v_home:
            return self.v_home.GetValue()
        elif self.v_limit:
            return not self.v_limit.GetValue()
        else:
            return True
        
    def OnLimitEnter(self, evt):
        self.Stop()
    
    def OnHomeEnter(self, evt):
        self.Stop()